<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>


    <meta http-equiv="content-type" content="text/html; charset=utf-8">
<title>JSON with Connection Manager</title>

<style type="text/css">
/*margin and padding on body element
  can introduce errors in determining
  element position and are not recommended;
  we turn them off as a foundation for YUI
  CSS treatments. */
body {
	margin:0;
	padding:0;
}
</style>

<link rel="stylesheet" type="text/css" href="../../build/fonts/fonts-min.css" />
<script type="text/javascript" src="../../build/yahoo-dom-event/yahoo-dom-event.js"></script>
<script type="text/javascript" src="../../build/json/json-min.js"></script>
<script type="text/javascript" src="../../build/connection/connection-min.js"></script>


<!--begin custom header content for this example-->

<!--end custom header content for this example-->

</head>

<body class="yui-skin-sam">


<h1>JSON with Connection Manager</h1>

<div class="exampleIntro">
	<p>A natural fit for the JSON Utility is working with the <a href="http://developer.yahoo.com/yui/connection/">Connection Manager</a>.  In this example, we'll request JSON data from the server using Connection Manager's <code>asyncRequest</code> and parse the resulting JSON string data for processing.</p>
<p>Click the Get Messages button to send the request to the server.</p>
			
</div>

<!--BEGIN SOURCE CODE FOR EXAMPLE =============================== -->

<div id="demo">
    <input type="button" id="demo_btn" value="Get Messages">

    <div id="demo_msg"></div>
</div>
<script type="text/javascript">
YAHOO.util.Event.on('demo_btn','click',function (e) {
    // Get the div element in which to report messages from the server
    var msg_section = YAHOO.util.Dom.get('demo_msg');
    msg_section.innerHTML = '';

    // Define the callbacks for the asyncRequest
    var callbacks = {

        success : function (o) {
            YAHOO.log("RAW JSON DATA: " + o.responseText);

            // Process the JSON data returned from the server
            var messages = [];
            try {
                messages = YAHOO.lang.JSON.parse(o.responseText);
            }
            catch (x) {
                alert("JSON Parse failed!");
                return;
            }

            YAHOO.log("PARSED DATA: " + YAHOO.lang.dump(messages));

            // The returned data was parsed into an array of objects.
            // Add a P element for each received message
            for (var i = 0, len = messages.length; i < len; ++i) {
                var m = messages[i];
                var p = document.createElement('p');
                var message_text = document.createTextNode(
                        m.animal + ' says "' + m.message + '"');
                p.appendChild(message_text);
                msg_section.appendChild(p);
            }
        },

        failure : function (o) {
            if (!YAHOO.util.Connect.isCallInProgress(o)) {
                alert("Async call failed!");
            }
        },

        timeout : 3000
    }

    // Make the call to the server for JSON data
    YAHOO.util.Connect.asyncRequest('GET',"assets/jsonConnect.php", callbacks);
});
</script>

<!--END SOURCE CODE FOR EXAMPLE =============================== -->

</body>
</html>
